package com.chenfan.process.entity.po;

import org.springframework.beans.BeanUtils;

import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.ccp.util.tool.UuidUtil;
import com.chenfan.process.entity.dto.FormDTO;
import com.chenfan.process.enums.BooleanEnum;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import lombok.Data;

/**
 * @author liuming
 * @date 2021/11/24
 */
@Data
@TableName(value = "process_form_attributes")
public class ProcessFormAttribute {

    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    /**
     * 表单的id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long formId;

    /**
     * 字段类型
     */
    private String fieldType;

    /**
     * 字段名
     */
    private String fieldName;
    /**
     * 字段Key
     */
    private String fieldKey;

    /**
     * 字段值
     */
    private String fieldValues;

    /**
     * 版本
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long version;

    /**
     * 是否必填
     */
    private Integer required;

    /**
     * 是否可编辑
     */
    private Integer editable;

    private Integer deletedFlag;

    @Data
    public static class Constants {
        public static final String FORM_ID = "form_id";
        public static final String FIELD_KEY = "field_key";
        public static final String REQUIRED = "required";

        private Constants() {}
    }

    public static ProcessFormAttribute ofDefault(FormDTO.FormAttribute e) {
        ProcessFormAttribute processFormAttribute = new ProcessFormAttribute();
        BeanUtils.copyProperties(e, processFormAttribute);
        processFormAttribute.setId(UuidUtil.generateId());
        processFormAttribute.setRequired(BooleanEnum.getValue(e.isRequired()));
        processFormAttribute.setEditable(BooleanEnum.getValue(e.isEditable()));
        return processFormAttribute;
    }
}
